package com.xiang.design.strategy.client;


import com.xiang.design.strategy.LogEnum;
import com.xiang.design.strategy.LogStrategy;
import com.xiang.design.strategy.StrategyFactory;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * 策略模式测试
 *
 * @author xiangtianlei@gogpay.cn
 * @date 2022-09-28
 */
@SpringBootTest()
public class StrategyModeTest {

    private final StrategyFactory strategyFactory = new StrategyFactory();

    /**
     * 方法一：通过策略模式获取对应的实现类(Map的方式)
     */
    @Test
    public void getLogByEnum() {
        LogStrategy file = strategyFactory.getLogByName(LogEnum.FILE);
        file.saveLog();
        System.out.println("------------------------------");
        LogStrategy local = strategyFactory.getLogByName(LogEnum.LOCAL);
        local.saveLog();
        System.out.println("------------------------------");
        LogStrategy remote = strategyFactory.getLogByName(LogEnum.REMOTE);
        remote.saveLog();
    }

    /**
     * 方法二：通过策略模式获取对应的实现类(Bean的方式)
     */
    @Test
    public void getLogByChannel() {
        LogStrategy file = strategyFactory.getLogByChannel(LogEnum.FILE.getChannel());
        file.saveLog();
        System.out.println("------------------------------");
        LogStrategy local = strategyFactory.getLogByChannel(LogEnum.LOCAL.getChannel());
        local.saveLog();
        System.out.println("------------------------------");
        LogStrategy remote = strategyFactory.getLogByChannel(LogEnum.REMOTE.getChannel());
        remote.saveLog();
    }
}
